package com.tansun.easycare.rule.excel.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;   
  
/**  
 * <p>  
 * Title: HTML相关的正则表达式工具类  
 * </p>  
 * <p>  
 * Description: 包括过滤HTML标记，转换HTML标记，替换特定HTML标记  
 * </p>  
 * <p>  
 * Copyright: Copyright (c) 2006  
 * </p>  
 *   
 * @author hejian  
 * @version 1.0  
 * @createtime 2006-10-16  
 */  
  
public class HtmlRegexpUtil {   
    private final static String regxpForHtml = "<([^>]*)>"; // 过滤所有以<开头以>结尾的标签  
  
    private final static String regxpForInputDate = ".*value=\"(.*)\" onfocus=.*"; // 找出input标签，日期专用
    
    private final static String regxpForInput = ".*value=\"(.*)\" style=.*"; // 找出input标签，一般通用
  
    private final static String regxpForOption = ".*<option value=\"(.*)\" selopt=\"1\".*"; // 找出option标签的选中属性   
  
    private final static String regxpForNumeric = "^[0-9]+(.[0-9]+)?$";	//判断为数字
    
    public static String filterHtml(String str) {   
    	str = str.replaceAll("\\){1,4}</option>","")//去除右括号选项
				.replaceAll("\\({1,4}</option>","")	//去除左括号选项
				.replaceAll("与|或</option>",""); 	//去除逻辑选项
		Pattern pattern = Pattern.compile(regxpForHtml); 
		Pattern inputPat1 = Pattern.compile(regxpForInputDate); 
		Pattern inputPat2 = Pattern.compile(regxpForInput);
    	Pattern patternOpt = Pattern.compile(regxpForOption);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();   
        boolean result1 = matcher.find();   
        String prevStr = "";
        while (result1) { 
        	String findStr = matcher.group(0);
        	String replaceStr = "";
        	if(findStr.contains("<input")) {	//判断为input标签
        		Matcher inputMat = null;
        		if(prevStr.contains("datatype=\"Date\"")) {//判断数据类型为日期
        			inputMat = inputPat1.matcher(findStr);
        		}else {
        			inputMat = inputPat2.matcher(findStr);
        		}
        		if( inputMat.find() ) {
        			replaceStr = inputMat.group(1).trim();
        			if( !replaceStr.matches(regxpForNumeric) ) {//判断字符串不是数字
	        			if("true".equals(replaceStr) || "false".equals(replaceStr)) {
		        				
	        			}else {
	        				replaceStr = "\"" + replaceStr + "\"";
	        			}
        			}
        		}
        	}else if(findStr.contains("<option")) {
        		Matcher optMat = patternOpt.matcher(findStr);
        		if(optMat.find()) {
        			replaceStr = optMat.group(1).trim();
        			if("and".equals(replaceStr)) {
        				replaceStr = " 与 ";
        			}else if( "or".equals(replaceStr) ) {
        				replaceStr = " 或 ";
        			}
        		}
        	}
        	int xint = sb.indexOf("xxx");
        	if( xint > -1 && StringUtils.isNotEmpty(replaceStr) ) {
        		sb.replace(xint, xint+3, replaceStr);
        		replaceStr = "";
        	}
			matcher.appendReplacement(sb, replaceStr);
            prevStr = findStr;
            result1 = matcher.find();  
        }   
        matcher.appendTail(sb);
		String res = sb.toString();
		res = res.replaceAll("&nbsp;", "")//去除空格
				.replaceAll(";", "")//去除分号
				.replaceAll("		\r\n", "").replaceAll("		", "")
				.replaceAll("	\r\n", "").replaceAll("	", "").replaceAll("\r\n\r\n", "").replaceAll("\n\n", "")//去除多余空白行
				.replaceAll("\"\"", "\",\"");//枚举值之间添加逗号
		return res.trim();
    }   
}